package medecin.android;

import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import static medecin.android.R.id.nom;
import modele.DAO;
import modele.Medecin;
import org.xml.sax.SAXException;

public class autreActivity extends ListActivity {

    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.vuedep);
        TextView label = (TextView) findViewById(R.id.label);
        Intent inter = getIntent();
        String dep = inter.getStringExtra("leDep");
        final List<Medecin> lesMeds;
        label.setText(dep);

        lesMeds = DAO.getLesMeds(dep);
        //Création de l'adapter
        MedAdapter adapter = new MedAdapter(lesMeds, this);
        setListAdapter(adapter);

        Button button = (Button) findViewById(R.id.filtrer);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                EditText edit = (EditText) findViewById(R.id.status);
                List<Medecin> lesMedsFiltres = new ArrayList<Medecin>();
                for (Medecin unMed : lesMeds) {
                    if (unMed.getNom().startsWith(edit.getText().toString())) {
                        lesMedsFiltres.add(unMed);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedsFiltres, getApplicationContext());
                setListAdapter(adapter);

            }

        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        String tel = ((Medecin) l.getItemAtPosition(position)).getTel();
        callIntent.setData(Uri.parse("tel:" + tel));
        startActivity(callIntent);
    }

}